#include"ThreadCache.h"
#include<vector>

void UnitThreadCache()
{
	ThreadCache tc;
	std::vector<void*> v;
	size_t size = 7;
	for (PAGE_ID i = 0; i < SizeClass::NumMoveSize(size) + 1; ++i)
		v.push_back(tc.Allocte(size));
	for (PAGE_ID i = 0; i < v.size(); ++i)
		printf("[%d]->%p\n", i, v[i]);
	for (auto ptr : v)
		tc.Deallocte(ptr,size);
}

void UnitTestSizeClass()
{
	cout << SizeClass::RoundUp(1) << endl;
	cout << SizeClass::RoundUp(127) << endl;
	cout << endl;
	cout << SizeClass::RoundUp(129) << endl;
	cout << SizeClass::RoundUp(1023) << endl;
	cout << endl;
	cout << SizeClass::RoundUp(1025) << endl;
	cout << SizeClass::RoundUp(8191) << endl;
	cout << endl;
	cout << SizeClass::RoundUp(8193) << endl;
	cout << SizeClass::RoundUp(65535) << endl;
	cout << endl;
	
	cout << SizeClass::ListIndex(1) << endl;
	cout << SizeClass::ListIndex(127) << endl;
	cout << endl;
	cout << SizeClass::ListIndex(129) << endl;
	cout << SizeClass::ListIndex(1023) << endl;
	cout << endl;
	cout << SizeClass::ListIndex(1025) << endl;
	cout << SizeClass::ListIndex(8191) << endl;
	cout << endl;
	cout << SizeClass::ListIndex(8193) << endl;
	cout << SizeClass::ListIndex(65535) << endl;
	cout << endl;
}

void UnitTestSystemAlloc()
{
	void* ptr = SystemAlloc(MAX_PAGESIZE - 1);
	PAGE_ID id = (PAGE_ID)ptr >> PAGE_SHIFT;
	void* ptrshift = (void*)(id << PAGE_SHIFT);
	cout << ptr << endl << ptrshift << endl << id << endl;
	char* obj1 = (char*)ptr;
	char* obj2 = (char*)ptr + 8;
	char* obj3 = (char*)ptr + 16;
	PAGE_ID id1 = (PAGE_ID)obj1 >> PAGE_SHIFT;
	PAGE_ID id2 = (PAGE_ID)obj2 >> PAGE_SHIFT;
	PAGE_ID id3 = (PAGE_ID)obj3 >> PAGE_SHIFT;
	cout << id1 << " " << id2 << " " << id3 << " " << endl;
}
//void UnitThreadCache()
//{
//	ThreadCache tc;
//	void* ptr1 = tc.Allocte(6);
//	void* ptr2 = tc.Allocte(6);
//	void* ptr3 = tc.Allocte(6);
//	void* ptr4 = tc.Allocte(6);
//	cout << ptr4 << " " << ptr3 << " " << ptr2 << " " << ptr1 << endl;
//}
#include "ConCurrent.h"
void fun1()
{
	std::vector<void*> v;
	size_t size = 7;
	for (size_t i = 0; i < 512; ++i)
		v.push_back(ConcurrentMalloc(size));
	v.push_back(ConcurrentMalloc(size));
	for (size_t i = 0; i < v.size(); ++i)
		printf("[%d]->%p\n", i, v[i]);
	for (auto ptr : v)
		ConcurrentFree(ptr);
}
void fun2(size_t n)
{
	std::vector<void*> v;
	size_t size = 7;
	for (size_t i = 0; i < SizeClass::NumMoveSize(size) + 1; ++i)
		v.push_back(ConcurrentMalloc(size));
	for (size_t i = 0; i < v.size(); ++i)
		printf("[%d]->%p\n", i, v[i]);
	for (auto ptr : v)
		ConcurrentFree(ptr);
}

//int main()
//{
//	UnitThreadCache();
//	UnitTestSizeClass();
//	UnitTestSystemAlloc();
//	fun1();
//
//	return 0;
//}